<?php
/**
 * This file is part of Swoft.
 *
 * @link https://swoft.org
 * @document https://doc.swoft.org
 * @contact group@swoft.org
 * @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
 */

namespace App\Controllers;


use App\Components\Auth;
use App\Components\Handle;
use App\Components\Tool;
use App\Components\YunClear;
use Swoft\Cache\Cache;
use Swoft\Http\Message\Server\Request;
use Swoft\Http\Server\Bean\Annotation\Controller;
use Swoft\Http\Server\Bean\Annotation\RequestMapping;

/**
 * 回调控制器
 * @Controller(prefix="/api")
 */
class ApiController
{
    /**
     * 图片审核回调
     * @param Request $request
     */
    public function img(Request $request){
        $sign = $request->input('sign');
        $json_data = $request->input('json_data');
        $json = json_decode($json_data,true);
        $audit_date = $json['audit_date'];
        if ($sign == YunClear::get_signature(YunClear::ImgAppID.','.$audit_date,YunClear::AppKey)){
                $relation_id = $json['relation_id'];
                $result = $json['result'];
                if (!$result){
                    $this->CallBack($relation_id);
                }
        }
    }

    /**
     * 文本回调地址
     * @param Request $request
     */
    public function text(Request $request){
        $sign = $request->input('sign');
        $json_data = $request->input('json_data');
        $json = json_decode($json_data,true);
        $audit_date = $json['audit_date'];
        if ($sign == YunClear::get_signature(YunClear::TextAppID.','.$audit_date,YunClear::AppKey)){
            $relation_id = $json['relation_id'];
            $result = $json['result'];
            if (!$result){
                $this->CallBack($relation_id);
            }
        }
    }

    /**
     * 接口回调
     * @param $relation_id
     */
    private function CallBack($relation_id){
        list($app,$type,$uid) = explode('-',$relation_id);
        $config = config('api.'.$app,[]);
        if ($config){
            $res = Auth::curl($config['url'].$type,Auth::params($config['api_key'],['uid'=>$uid]));
            Tool::log("{$relation_id},{$res}",'callback'.date('Ymd').'.log');
        }else{
            Tool::log("{$app}配置不存在,{$relation_id}",'callback'.date('Ymd').'.log');
        }
    }
}